package com.horoscope.astrology.zodiac.signs.free.view

import `in`.srain.cube.views.ptr.PtrFrameLayout
import `in`.srain.cube.views.ptr.PtrUIHandler
import `in`.srain.cube.views.ptr.indicator.PtrIndicator
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import com.horoscope.astrology.zodiac.signs.free.R
import com.horoscope.astrology.zodiac.signs.free.utils.MyAnimationUtils
import kotlinx.android.synthetic.main.custom_ptr_header.view.*

/**
 * 下来刷新头部布局
 */
class CustomPtrHeader @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null,
    defStyleRes: Int = 0
) :
    FrameLayout(context, attributeSet, defStyleRes), PtrUIHandler
    , CustomerPtrUIHandlerHook.OnPtrUIHandlerHookCallback {
    override fun onPtrUIHandlerHookStart() {
        mLoading?.clearAnimation()
        mLoading?.visibility = View.INVISIBLE
        image_bg?.visibility = View.INVISIBLE
        mTextView?.clearAnimation()
        if (success) {
            mTextView?.text = resources.getString(R.string.label_up_to_date)
        } else {
            mTextView?.text = resources.getString(R.string.label_load_fail)
        }
        mTextView?.postDelayed({
            handlerHook?.resume()
        }, 1000)
    }

    private var handlerHook: CustomerPtrUIHandlerHook? = null
    private var mLoading: ImageView? = null
    private var mTextView: TextView? = null
    private var success = true

    init {
        val view = LayoutInflater.from(context).inflate(R.layout.custom_ptr_header, this)
        mLoading = view.findViewById(R.id.image_view_loading)
        mTextView = view.findViewById(R.id.text_view_s)


    }

    fun setSuccess(success: Boolean) {
        this.success = success
    }

    override fun onUIRefreshComplete(frame: PtrFrameLayout?) {
        mLoading?.clearAnimation()
        image_bg?.clearAnimation()
        image_bg?.visibility = View.INVISIBLE
    }

    private fun initAnimation() {

        image_bg?.visibility = View.VISIBLE
        mLoading?.startAnimation(MyAnimationUtils.loadingAnimation())
    }

    override fun onUIPositionChange(
        frame: PtrFrameLayout?,
        isUnderTouch: Boolean,
        status: Byte,
        ptrIndicator: PtrIndicator?
    ) {

        if (!isUnderTouch || status == PtrFrameLayout.PTR_STATUS_LOADING || status == PtrFrameLayout.PTR_STATUS_COMPLETE) {
            return
        }
        image_bg?.visibility = View.VISIBLE
        val currentPos = ptrIndicator?.currentPosY
        mLoading?.rotation = currentPos?.toFloat()!!
    }

    override fun onUIRefreshBegin(frame: PtrFrameLayout?) {
        initAnimation()

    }

    override fun onUIRefreshPrepare(frame: PtrFrameLayout?) {
        mLoading?.visibility = View.VISIBLE
        mTextView?.text = ""
        handlerHook = CustomerPtrUIHandlerHook(this)
        frame?.setRefreshCompleteHook(handlerHook)
    }

    override fun onUIReset(frame: PtrFrameLayout?) {
    }

}